<?php
/**
 * Pages_StudentController
 *
 * Working with students
 */
class Pages_StudentController extends Zend_Controller_Action
{

    /**
     * Check if current student have a task record in DB and redirect to according action
     * (choosing task or viewing a selected one)
     *
     */
    public function indexAction()
    {
        $studentId = $this->_getParam('studentId');
        $tableStudent = new Zend_Student();
        $student = $tableStudent->getStudent($studentId);
        if ($student != NULL) 
        {
            $task = $tableStudent->checkStudent($studentId);
            if ($task == NULL) 
            {
                $this->_redirect('/pages/student/choose/' . $studentId);
            }
            else $this->_redirect('/pages/student/' . $studentId . '/view');
        }
        else $this->view->message='Даного студента не знайдено.';
    }


    /**
     * Choosing task from available ones
     *
     */
    public function chooseAction()
    {
        $studentId = $this->_getParam('studentId');
        $tableStudent = new Zend_Student();
        $student = $tableStudent->getStudent($studentId);
        $taskId = (int)$this->_getParam('taskId');
        if ($taskId != NULL)
        {
            $check=$tableStudent->insertTask($studentId, $taskId);
            if ($check == 1)
            $this->_redirect('/pages/student/' . $studentId . '/view');
            else echo 'Помилка бази даних';
        }
        else
        {
            if ($student != NULL) 
            {
                $this->view->studentId = $studentId;
                $groupId=$student['GROUP_ID'];
                $fullStudent=$student['NAME_LAST'] . ' ' . $student['NAME_FIRST'] . ' ' . $student['NAME_MIDDLE'];
                $this->view->student = $fullStudent;
                $this->view->group = $student['NAME'];
                $teachId=$student['TEACHER_ID'];
                $teacher = $tableStudent->getTeacher($teachId);
                $fullTeacher=$teacher['LAST_NAME'] . ' ' . $teacher['FIRST_NAME'] . ' ' . $teacher['MIDDLE_NAME'];
                $this->view->teacher = $fullTeacher;
                $tasks=$tableStudent->availableTasks($groupId);
                $this->view->tasks=$tasks;
             }
             else 
             $this->view->message='Даного студента не знайдено.';
        }
    }


    /**
     * Show student's task
     *
     *
     */
    public function viewAction()
    {
        $studentId = $this->_getParam('studentId');
        $tableStudent = new Zend_Student();
        $student = $tableStudent->getStudent($studentId);
        if ($student != NULL) 
        {
            $fullStudent=$student['NAME_LAST'] . ' ' . $student['NAME_FIRST'] . ' ' . $student['NAME_MIDDLE'];
            $this->view->student = $fullStudent;
            $this->view->group = $student['NAME'];
            $teachId=$student['TEACHER_ID'];
            $teacher = $tableStudent->getTeacher($teachId);
            $fullTeacher=$teacher['LAST_NAME'] . ' ' . $teacher['FIRST_NAME'] . ' ' . $teacher['MIDDLE_NAME'];
            $this->view->teacher = $fullTeacher;
            $task = $tableStudent->checkStudent($studentId);
            $taskId = $task['ID'];
            $task_numbers = $tableStudent->getTask($taskId);
            $this->view->n1 = $task_numbers[0] ['name'];
            $this->view->n2 = $task_numbers[1] ['name'];
            $this->view->img1 = $task_numbers [0] ['image'];
            $this->view->img2 = $task_numbers [1] ['image'];
            $this->view->v1 = $task_numbers[0] ['var'];
            $this->view->v2 = $task_numbers[1] ['var'];
            $this->view->vd1 = $task_numbers [0] ['var_data'];
            $this->view->vd2 = $task_numbers [1] ['var_data'];
       }
    }


    /**
     * Login Action
     *
     */
    public function loginAction() 
    {
      $form = new Zend_LoginStudent();
      $form->submit->setLabel('Login');
      $this->view->form = $form;
      if ($this->_request->isPost())
      {
          $formData = $this->_request->getPost();
          if ($form->isValid($formData)) 
          {
              $number=(int)$form->getValue('number');
              $tableStudent = new Zend_Student();
              $studentId = $tableStudent->getId($number);
              if ($studentId != NULL) 
              {
                  $this->_redirect('/pages/student/' . $studentId);
              } else 
              echo '<center>Такого студента не знайдено</center>';
           } else 
           {
            $form->populate($formData);
           }
       }
    }
}

